/*
 * NewAccountDialog.java
 *
 *  
 */
package org.msb.finance.app;

import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.NORTHWEST;
import static java.awt.GridBagConstraints.SOUTH;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * 
 * 
 * @author boudream
 * 
 */
@SuppressWarnings("serial")
public class NewAccountDialog extends JDialog {

	/*
	 * 
	 */
	private JComboBox accountTypeCombo;

	/*
	 * 
	 */
	private JTextField accountNameField;

	/**
	 * @param owner
	 */
	public NewAccountDialog(Frame owner) {
		super(owner);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JDialog#dialogInit()
	 */
	@Override
	protected void dialogInit() {
		super.dialogInit();

		setContentPane(createContentPane());
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setModal(true);
		setTitle(FinanceApp.getMessageResources().getString("dialog.newaccount.title")); //$NON-NLS-1$

		pack();
	}

	private JComponent createContentPane() {
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		JLabel label = new JLabel(FinanceApp.getMessageResources().getString("dialog.newaccount.accounttype.caption")); //$NON-NLS-1$
		panel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(0, 0, 0, 0), 0, 0));

		panel.add(getAccountTypeCombo(), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(0,
				20, 0, 0), 0, 0));

		label = new JLabel(FinanceApp.getMessageResources().getString("dialog.newaccount.accountname.caption")); //$NON-NLS-1$
		panel.add(label, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(10, 0, 0, 0), 0, 0));

		panel.add(getAccountNameField(), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, NORTHWEST, NONE, new Insets(0,
				20, 0, 0), 0, 0));

		panel.add(createButtonPanel(), new GridBagConstraints(0, 4, 1, 1, 0.0, 1.0, SOUTH, NONE,
				new Insets(10, 0, 0, 0), 0, 0));

		return panel;
	}

	/*
	 * 
	 */
	private JComboBox getAccountTypeCombo() {
		if (null == this.accountTypeCombo) {
			this.accountTypeCombo = new JComboBox(AccountTypeEnum.getAllTypes());
			this.accountTypeCombo.setRenderer(new AccountTypeComboRenderer());
		}

		return this.accountTypeCombo;
	}

	/*
	 * 
	 */
	private JTextField getAccountNameField() {
		if (null == this.accountNameField) {
			this.accountNameField = new JTextField(30);
		}

		return this.accountNameField;
	}

	/*
	 * 
	 */
	private JComponent createButtonPanel() {
		JPanel panel = new JPanel(new GridLayout(1, 2, 5, 0));

		ActionListener al = new ActionListener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent event) {
				String command = event.getActionCommand();

				if (command.equals("cancel")) { //$NON-NLS-1$
					NewAccountDialog.this.accountTypeCombo.setSelectedIndex(-1);
					NewAccountDialog.this.accountNameField.setText(null);
				}

				setVisible(false);
			}
		};

		final Action okAction = new Action(al, FinanceApp.getMessageResources(),
				"button.ok", "ok", false, false, new NewAccountActionEnabledCallback()); //$NON-NLS-1$ //$NON-NLS-2$
		JButton okButton = new JButton(okAction);

		panel.add(okButton);

		JButton cancelButton = new JButton(new Action(al, FinanceApp.getMessageResources(), "button.cancel", "cancel", //$NON-NLS-1$ //$NON-NLS-2$
				true, false, null));

		panel.add(cancelButton);

		this.accountTypeCombo.addItemListener(new ItemListener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
			 */
			public void itemStateChanged(ItemEvent e) {
				okAction.stateChanged(new ChangeEvent(e.getSource()));
			}
		});

		this.accountNameField.getDocument().addDocumentListener(new DocumentListener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
			 */
			public void changedUpdate(DocumentEvent e) {
				okAction.stateChanged(new ChangeEvent(NewAccountDialog.this.accountNameField));
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
			 */
			public void insertUpdate(DocumentEvent e) {
				okAction.stateChanged(new ChangeEvent(NewAccountDialog.this.accountNameField));
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
			 */
			public void removeUpdate(DocumentEvent e) {
				okAction.stateChanged(new ChangeEvent(NewAccountDialog.this.accountNameField));
			}
		});

		return panel;
	}

	/**
	 * 
	 * @return s
	 */
	public AccountTypeEnum getAccountType() {
		return (AccountTypeEnum) this.accountTypeCombo.getSelectedItem();
	}

	/**
	 * 
	 * @return s
	 */
	public String getAccountName() {
		return this.accountNameField.getText();
	}

	/*
	 * 
	 */
	private class AccountTypeComboRenderer extends DefaultListCellRenderer {

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object,
		 *      int, boolean, boolean)
		 */
		@Override
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
				boolean cellHasFocus) {
			return super.getListCellRendererComponent(list, ((AccountTypeEnum) value).getName(), index, isSelected,
					cellHasFocus);
		}
	}

	/*
	 * 
	 */
	private class NewAccountActionEnabledCallback implements ActionEnabledCallback {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.msb.finance.app.ActionEnabledCallback#isEnabled()
		 */
		public boolean isEnabled() {
			return -1 < NewAccountDialog.this.accountTypeCombo.getSelectedIndex()
					&& 0 < NewAccountDialog.this.accountNameField.getText().length();
		}

	}
}
